//
//  FileDownLoadService.m
//  Bus_iPhone
//
//  Created by 黄修勇 on 12-9-2.
//
//

#import "FileDownLoadService.h"

@implementation FileDownLoadService


-(id)init{

    self=[super init];
    if (self) {
        queue=[[ASINetworkQueue alloc]init];
        [queue setMaxConcurrentOperationCount:FILE_DOWN_DEFAULT_MAX_COUNT];
        [queue setShouldCancelAllRequestsOnFailure:NO];
        [queue go];
    }
    return self;
}

-(void)dealloc{

    [queue cancelAllOperations];
    [queue release];
    [super dealloc];
}


-(void)downLoadFileWithURL:(NSURL *)url tempPath:(NSString *)tempPath filePath:(NSString *)path progressDelegate:(id)delegate{

    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    request.timeOutSeconds=DOWN_FILE_DEFAULT_TIME_OUT;
    [request setAllowResumeForFileDownloads:YES];
    [request setTemporaryFileDownloadPath:tempPath];
    [request setDownloadDestinationPath:path];
    [request setDownloadProgressDelegate:delegate];
    [queue addOperation:request];
    
}

-(BOOL)cancelDownLoadFileWithURL:(NSURL *)url{

    NSArray *array=[queue operations];
    for (ASIHTTPRequest *request in array) {
        if ([request.url isEqual:url]) {
            [request clearDelegatesAndCancel];
            return YES;
        }
    }
    
    return NO;
}
@end
